package com.zwj.user.controller.user;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zwj.common.R;
import com.zwj.common.entity.DemandersInsideAndOutsideTheSchool;
import com.zwj.common.entity.User;
import com.zwj.common.service.DemandersInsideAndOutsideTheSchoolService;
import com.zwj.user.security.PasswordSecurity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@RestController
@Slf4j
@RequestMapping("/demandersInsideAndOutsideTheSchool")
public class DemandersInsideAndOutsideTheSchoolController {

    @Resource
    private DemandersInsideAndOutsideTheSchoolService demandersService;

    @PostMapping("/register")
    public R<Object> register(@RequestBody DemandersInsideAndOutsideTheSchool demanders) {

        return R.success(demandersService.register(demanders));
    }

    @GetMapping("/usernameIsRegister/{username}")
    public R<String> usernameIsRegister(@PathVariable("username") String username) {

        DemandersInsideAndOutsideTheSchool one = demandersService.getOne(new LambdaQueryWrapper<DemandersInsideAndOutsideTheSchool>()
                .eq(DemandersInsideAndOutsideTheSchool::getUsername, username));

        if(ObjectUtil.isNotEmpty(one))
            return R.error("当前用户名已被注册，请换一个用户名");

        return R.success("ok");
    }

    //查询是否此号码已经注册
    @GetMapping("/phoneNumberIsRegister/{phoneNumber}")
    public R<String> phoneNumberIsRegister(@PathVariable("phoneNumber") String phoneNumber){

        LambdaQueryWrapper<DemandersInsideAndOutsideTheSchool> demandersWrapper =
                new LambdaQueryWrapper<DemandersInsideAndOutsideTheSchool>()
                        .eq(DemandersInsideAndOutsideTheSchool::getPhoneNumber,
                                phoneNumber);
        DemandersInsideAndOutsideTheSchool one = demandersService
                .getOne(demandersWrapper);
        if(ObjectUtil.isNotEmpty(one))
            return R.error("此号码已被注册，一个号码只能注册一个账号!");

        return R.success("ok");
    }



    @GetMapping("/getSchoolAdministratorDetails/{id}")
    public R<DemandersInsideAndOutsideTheSchool> getSchoolAdministratorDetails(@PathVariable("id") Long id) {

        return R.success(demandersService.getById(id));
    }

    //修改用户名，密码，注册手机号
    @PutMapping("/changeUser")
    public R<String> changeUser(@RequestBody User user) {

        Long loginId = (Long) StpUtil.getLoginId();

        DemandersInsideAndOutsideTheSchool demanders = new DemandersInsideAndOutsideTheSchool();

        demanders.setUpdateTime(LocalDateTime.now());

        if(StrUtil.isNotBlank(user.getUsername()))
            demanders.setUsername(user.getUsername());

        if(StrUtil.isNotBlank(user.getPassword())) {
            String pw = PasswordSecurity.Md5Encoding(user.getPassword());
            demanders.setPassword(pw);
        }

        if(StrUtil.isNotBlank(user.getPhoneNumber()))
            demanders.setPhoneNumber(user.getPhoneNumber());

        demanders.setId(loginId);

        boolean b = demandersService.updateById(demanders);

        if(b)
            return R.success("修改成功!");

        return R.error("修改失败!");
    }


}
